﻿using ProtoBuf.Message;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public enum FightState
    {
        None = 0,
        CreateShip = 1, // 创建船
        ShipComming = 2,// 船行驶
        EnemyEnter = 3, // 敌人入场

        BeginFight = 4, // 开始战斗 英雄入场 播放特效
        WaitForFightEnd = 5,//等待战斗结束
        EndFighting = 6,// 战斗结束 英雄退场

        ResultPage = 7, //弹出结算面板
        CloseResultPage = 8, //关闭结算面板

        SceneResult = 9,// 播放炮弹 

        DirectResult = 10,//直接结算阶段
        Wating = 11, //通用回调等待
    }
    public enum CampState
    {
        ALLHAVE,
        AllNone,
        LeftNone,
        RightNone,
    }
    /// <summary>
    /// 构建一个战斗演算的数据
    /// </summary>
    public class FightPackage
    {
        public long playerGuid;//战斗所属玩家Id
        public float fightShowTime;//战斗的展现时长
        public SCAutoChessMatchRes oneMatchRes;
        public SCAutoChessFightRes oneFightResult;
    }
    public enum SimulationFight
    {
        FightStating,//战斗阶段等待中
        OnFightState //战斗阶段判断中
    }
    /// <summary>
    /// 战斗实体类， 模拟不同玩家的战斗状态
    /// </summary>
    public class FightEntity 
    {
        private FightEntityCtr ctr;

        private long playerGuid ;//战斗所属玩家Id
        private float fightShowTime;//战斗的展现时长
        private float curFightPassTime;//当前战斗已经流逝的时间
        private SCAutoChessMatchRes oneMatchRes;
        private SCAutoChessFightRes oneFightResult;

        private CampState campState = CampState.ALLHAVE;// 阵营状态
        private FightState curState = FightState.None;  //当前的战斗阶段

        private SimulationFight simulationState = SimulationFight.OnFightState;
        private float waitingTime = 0;
        private Action simulateAction;

        private float _passTime = 0;
        private bool hasSendFinished = false; //是否发送过完成
        public long PlayerGUID
        {
            get { return playerGuid; }
        }
        public float FightShowTime
        {
            get { return fightShowTime; }
        }
        public CampState ThisCampState
        {
            get { return campState; }
        }
        public FightState CurFightState
        {
            get { return curState; }
        }

        private void SetCurState(FightState state)
        {
           // curState = state;
            if (curState == FightState.DirectResult && state == FightState.DirectResult)
            {
                //防止重复处理
            }
            else
            {
                curState = state;
                if (curState == FightState.DirectResult)
                {
                   
                }
            }
        }
        /// <summary>
        /// 战斗开始
        /// </summary>
        public void OnFightCreate(FightPackage package, FightEntityCtr ctr)
        {
            this.playerGuid = package.playerGuid;
            this.fightShowTime = package.fightShowTime;
            this.oneMatchRes = package.oneMatchRes;
            this.oneFightResult = package.oneFightResult;
            this.ctr = ctr;

            CalcCampState();
            SetCurState(FightState.CreateShip);
            simulationState = SimulationFight.OnFightState;
            hasSendFinished = false;
        }
        private void CalcCampState()
        {
            if (oneMatchRes == null || oneMatchRes.left == null || oneMatchRes.left.resultChess == null ||
               oneMatchRes.right == null || oneMatchRes.right.resultChess == null)
            {
                if (oneMatchRes == null)
                {
                    Debug.LogError("数据异常，查看网络log消息");
                }
                else {
                    Debug.LogError("服务器数据异常，查看网络log消息");
                }
                campState = CampState.AllNone;
                return;
            }

            if (oneMatchRes.left.resultChess.Count > 0 && oneMatchRes.right.resultChess.Count > 0 )
            {
                campState = CampState.ALLHAVE;
            }
            else if (oneMatchRes.left.resultChess.Count > 0 && oneMatchRes.right.resultChess.Count <= 0)
            {
                campState = CampState.RightNone;
            }
            else if (oneMatchRes.left.resultChess.Count <= 0 && oneMatchRes.right.resultChess.Count > 0)
            {
                campState = CampState.LeftNone;
            }
            else
            {
                campState = CampState.AllNone;
            }
        }
        public void ComplateFightFinished()
        {
            SetCurState(FightState.DirectResult);
            simulationState = SimulationFight.OnFightState;
        }
        /// <summary>
        /// 走过战斗结果了，再次进来的话，就直接是显示结果了
        /// </summary>
        public void ComplateFightToResult()
        {
            SetCurState(FightState.DirectResult);
            simulationState = SimulationFight.OnFightState;
            hasSendFinished = true;
        }
        /// <summary>
        /// 所有战场玩家的状态阶段模拟
        /// </summary>
        public void OnFightTick()
        {
            _passTime += Time.unscaledDeltaTime;
            if (simulationState == SimulationFight.FightStating)
            {
                waitingTime -= Time.fixedUnscaledTime;
                if (waitingTime <= 0)
                {
                    if (simulateAction != null)
                    {
                        simulateAction();
                        simulateAction = null;
                    }
                }
            }
            else if (curState == FightState.CreateShip)
            {
                SetCurState(FightState.ShipComming);
            }
            else if (curState == FightState.ShipComming)
            {
                waitingTime = AutoChessFieldViewCtr.Ins.SceneConfig.shipCome.AnimRealTime;
                simulationState = SimulationFight.FightStating;
                simulateAction = () =>
                {
                    simulationState = SimulationFight.OnFightState;
                    SetCurState(FightState.EnemyEnter);
                };
            }
            else if (curState == FightState.EnemyEnter)
            {
                if (campState == CampState.ALLHAVE)
                {
                    simulationState = SimulationFight.FightStating;
                    waitingTime = AutoChessFieldViewCtr.Ins.SceneConfig.showHeroTime;
                    simulateAction = () =>
                    {
                        simulationState = SimulationFight.OnFightState;
                        SetCurState(FightState.BeginFight);
                    };
                }
                else
                {
                    simulationState = SimulationFight.FightStating;
                    waitingTime = AutoChessFieldViewCtr.Ins.SceneConfig.showHeroTime;
                    simulateAction = () =>
                    {
                        simulationState = SimulationFight.OnFightState;
                        SetCurState(FightState.SceneResult);
                    };
                }
            }
            else if (curState == FightState.BeginFight)
            {
                SetCurState(FightState.WaitForFightEnd);
            }
            else if (curState == FightState.WaitForFightEnd) //战斗时间结束或超时 后台直接 状态切换到显示结果得了
            {
                //战斗UI显示倒计时结束 或者 战斗超时 结束
                if (ctr.CurFightPassTime <= 0)
                {
                    SetCurState(FightState.SceneResult);
                }
            }
            else if (curState == FightState.SceneResult)
            {
                //是自己，且没有发送过 ，且当前不是播自己的战场 这个时候 直接算自己结束了 ， 后台状态运算发送战斗结束通知
                if (hasSendFinished == false &&
                    playerGuid == StaticData.playerData.playerId && 
                    ctr.CurFightEntity.PlayerGUID != StaticData.playerData.playerId)
                {
                    //AutoChess.AutoChessManager.Ins.SendCSAutoChessFightFinish();
                    //Debug.LogWarning("后台提交战斗完成");
                }
                SetCurState(FightState.DirectResult);//完了就直接算完了
            }
        }

        /// <summary>
        /// 清除战斗数据
        /// </summary>
        public void OnDestroyFight()
        {
        }
    }
}
